package club.qwertyzzz;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.Buffer;

/**
 * 将游戏的资源加载到内存中
 */
public class ResourceMgr {

    /**
     * 玩家1坦克图片
     */
    public static BufferedImage player1TankL;
    public static BufferedImage player1TankD;
    public static BufferedImage player1TankR;
    public static BufferedImage player1TankU;

    /**
     * 玩家2坦克图片
     */
    public static BufferedImage player2TankL;
    public static BufferedImage player2TankD;
    public static BufferedImage player2TankR;
    public static BufferedImage player2TankU;

    public static BufferedImage tankMissile;

    /**
     * 敌方坦克种类1图片
     */
    public static BufferedImage enemy1TankL;
    public static BufferedImage enemy1TankD;
    public static BufferedImage enemy1TankR;
    public static BufferedImage enemy1TankU;

    /**
     * 敌方坦克种类2图片
     */
    public static BufferedImage enemy2TankL;
    public static BufferedImage enemy2TankD;
    public static BufferedImage enemy2TankR;
    public static BufferedImage enemy2TankU;

    /**
     * 敌方坦克种类3图片
     */
    public static BufferedImage enemy3TankL;
    public static BufferedImage enemy3TankD;
    public static BufferedImage enemy3TankR;
    public static BufferedImage enemy3TankU;

    /* 爆炸效果图片 */
    public static BufferedImage[] blast = new BufferedImage[8];

    static {
        try {
            player1TankL = loadImage("img/p1tankL.gif");
            player1TankD = loadImage("img/p1tankD.gif");
            player1TankR = loadImage("img/p1tankR.gif");
            player1TankU = loadImage("img/p1tankU.gif");

            player2TankL = loadImage("img/p2tankL.gif");
            player2TankD = loadImage("img/p2tankD.gif");
            player2TankR = loadImage("img/p2tankR.gif");
            player2TankU = loadImage("img/p2tankU.gif");

            tankMissile = loadImage("img/tankmissile.gif");

            enemy1TankL = loadImage("img/enemy1L.gif");
            enemy1TankD = loadImage("img/enemy1D.gif");
            enemy1TankR = loadImage("img/enemy1R.gif");
            enemy1TankU = loadImage("img/enemy1U.gif");

            enemy2TankL = loadImage("img/enemy2L.gif");
            enemy2TankD = loadImage("img/enemy2D.gif");
            enemy2TankR = loadImage("img/enemy2R.gif");
            enemy2TankU = loadImage("img/enemy2U.gif");

            enemy3TankL = loadImage("img/enemy3L.gif");
            enemy3TankD = loadImage("img/enemy3D.gif");
            enemy3TankR = loadImage("img/enemy3R.gif");
            enemy3TankU = loadImage("img/enemy3U.gif");

            for(int i = 0; i < 8; i++) {
                blast[i] = loadImage("img/blast" + (i + 1) + ".gif");
            }
        } catch (IOException e) {
            System.out.println("资源加载失败");
        }
    }

    public static BufferedImage loadImage(String path) throws IOException {
        return ImageIO.read(ResourceMgr.class.getClassLoader().getResourceAsStream(path));
    }

    /**
     * 根据坦克的方向 和 坦克的类型获得坦克的图片
     *
     * @param dir
     * @param type
     */
    public static BufferedImage getTankImage(Dir dir, TankType type) {
        BufferedImage resultImage = null;
        try {
            //1. 根据dir和的type拼接出属性名称的字符串
            String propertyName = type.name() + "Tank" + dir.name().substring(0, 1);

            //2. 利用反射获取具体图片的属性
            Field[] fields = ResourceMgr.class.getDeclaredFields();
            for (Field f: fields) {
                if(f.getName().equals(propertyName)) {
                    resultImage = (BufferedImage)f.get(ResourceMgr.class);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            System.out.println("获取图片资源失败!");
        }
        return resultImage;
    }

    public static void playFireSound() {
        new Audio("img/fire.wav").start();
    }

    public static void playBlastSound() {
        new Audio("img/blast.wav").start();
    }

    public static void playStartSound() {
        new Audio("img/start.wav").start();
    }
}
